package com.sjr.easy.chat.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sjr.easy.chat.model.Contact;
import com.sjr.easy.chat.service.ContactService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.sjr.easy.chat.repo.ContactsRepo;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@Transactional
public class ContactServiceImpl implements ContactService {

    @Autowired
    private ContactsRepo contactsRepo;

    @Override
    public void addContact(Contact contact) {
        log.info("contact:{}", contact);
        contactsRepo.save(contact);
    }

    @Override
    public Contact getContact(Long userId, Long contactUserId) {
        log.info("userId:{}, contactUserId:{}", userId, contactUserId);
        Objects.requireNonNull(userId);
        Objects.requireNonNull(contactUserId);
        Contact contact = new Contact();
        contact.setUserId(userId);
        contact.setContactUserId(contactUserId);
        QueryWrapper<Contact> queryWrapper = new QueryWrapper<>(contact);
        return contactsRepo.getOne(queryWrapper);
    }

    @Override
    public List<Contact> selectContactsListByUserId(Long userId) {
        Contact contact = new Contact();
        contact.setUserId(userId);
        LambdaQueryWrapper<Contact> queryWrapper = new LambdaQueryWrapper<>(contact);
        return contactsRepo.list(queryWrapper);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        log.info("idList:{}", JSON.toJSONString(idList));
        if (CollectionUtils.isEmpty(idList)) {
            return;
        }
        contactsRepo.removeByIds(idList);
    }
}
